<?php

namespace app\api\controller;

use app\admin\model\Banner;
use app\admin\model\Msgsned;
use app\common\controller\Api;
use app\common\model\OrderMore;
use app\common\model\Packages;
use app\common\model\PackagesUserStatus;
use app\common\model\User;
use app\common\model\UserCommission;
use app\common\model\UserPackagesConfig;
use fast\Random;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['ceshi','orderceshi','goodsdetails','imagebase64','jiesuan','digui','laqu','zqRange','msgsend','msgsendInfo','banner'];
    protected $noNeedRight = ['*'];

    public function cse($data)
    {
//        dump($data);exit();
        $data = json_decode($data, true);
        if ($data) {
            $orderInfo = \app\common\model\Order::where(['order_id' => $data['orderId']])->find();
            if ($orderInfo) {
                if ($orderInfo['order_status'] == 0 || $orderInfo['order_status'] == 5) {
//                        echo "订单已更新";
                    $orderInfo->order_info_json = json_encode($data, true);
                    $orderInfo->save();
                    return;
                }
                if (!empty($orderInfo['packages'])) {
                    if ($orderInfo['packages']['platform_type'] == 2) {
                        $secret = config('site.hao2_secret');
                    } else {
                        $secret = config('site.hao3_secret');
                    }
                }
                $send_sign = $data['sign'];

                unset($data['sign']);

                ksort($data);//字典排序,升序排列
                $str='';
                foreach ($data as $k => $vv) {
                    $str.= $k.$vv;
                }
                $sign = strtoupper(md5($secret.$str.$secret));//签名
                dump($sign);
                dump($send_sign);
                dump($data);
//                $sign = strtoupper(md5($secret.$str.$secret));//签名
//                dump($secret);
//                dump($orderInfo['id']);
//                dump($orderInfo['packages']['platform_type'] );
//                dump($send_sign);

                exit();
//                if ($sign == $send_sign) {
                    if (isset($data['state'])) {
                        if ($data['state'] == 0) {
                            $orderInfo->order_status = 3;
                        } else {
                            if ($data['state'] == 11) {
                                $orderInfo->order_status = 2;
                            } else {
                                if ($data['state'] == 1 || $data['state'] == 10) {
                                    $orderInfo->order_status = 1;
                                } else {
                                    if ($data['state'] == 6) {
                                        $packagesModel           = new \app\admin\model\Packages();
                                        $packageInfo             = $packagesModel->where(['id' => $orderInfo['goods_id']])->find();
                                        $orderInfo->order_status = 5;
                                        if ($orderInfo->is_agency == 1) {
                                            $orderInfo->settlement_time = time();
                                            $orderInfo->is_hexiao       = 2;
                                            orderfencheng($orderInfo->id, $orderInfo->goods_id);
                                        } else {
                                            $userModel = new \app\common\model\User();
                                            if ($orderInfo->audit_status != 2) {
                                                $orderInfo->order_status             = 4;
                                                $orderInfo->is_tripartite_settlement = 1;
                                            } else {
                                                $orderInfo->settlement_time = time();
                                                $orderInfo->is_hexiao       = 2;
                                                if ($orderInfo->is_more == 2) {
                                                    $orderInfo->two_js_time = settlement_time($orderInfo->settlement_time);
                                                    $huafei                 = $packageInfo['bxhf_text'];
                                                    $orderInfo->is_hexiao   = 5;
                                                    $orderInfo->more_num    = 1;
                                                    if ($packageInfo['day'] != 6) {
                                                        $orderInfo->hexiao_next_time = 7 * 86400 + time();
                                                    } else {
                                                        $orderInfo->hexiao_next_time = $orderInfo->two_js_time;
                                                    }
                                                } else {
                                                    $huafei = bcadd($packageInfo['ciyue_set_meal'],
                                                        $packageInfo['bxhf_text']);
                                                }
                                                if ($huafei) {
                                                    $userModel::money($huafei, $orderInfo->user_id, '话费报销',
                                                        $orderInfo->id,
                                                        $packageInfo['id']);
                                                    $orderInfo->baoxiao_time   = time();
                                                    $orderInfo->baoxiao_status = 2;
                                                }
                                                $v                     = $orderInfo;
                                                $userInfo              = $userModel->where(['id' => $v['user_id']])->find();
                                                $zigou_commission      = $packageInfo['one_commission'] ?? 0;
                                                $one_commission        = $packageInfo['junior_contribution'] ?? 0;
                                                $tjyj                  = $packageInfo['junior_contribution_tow'] ?? 0;
                                                $pplots_profit         = $packageInfo['pplots_profit'] ?? 0;
                                                $pplots_yunying_profit = $packageInfo['pplots_yunying_profit'] ?? 0;
                                                if ($packageInfo['duoci'] || $v->is_more == 2) {
                                                    jiangli($v, $packageInfo);
                                                    if ($v['parent_id']) {
                                                        jiangli($v, $packageInfo, 2);
                                                    }
                                                    if ($v['two_id']) {
                                                        jiangli($v, $packageInfo, 3);
                                                    }
                                                    //区县
                                                    if ($v['agency_area_id']) {
                                                        jiangli($v, $packageInfo, 4);
                                                    }
                                                    //小区
                                                    if ($v['plot_id']) {
                                                        jiangli($v, $packageInfo, 5);
                                                    }
                                                } else {
                                                    if ($userInfo) {
                                                        if ($zigou_commission) {
                                                            $userModel::money($zigou_commission, $v['user_id'],
                                                                '自购奖励', $v['id'], $packageInfo['id']);
                                                        }
                                                        if ($userInfo['parent_id'] && $one_commission) {
                                                            $userModel::money($one_commission, $userInfo['parent_id'],
                                                                '推荐有奖', $v['id'], $packageInfo['id']);
                                                        }
                                                        if ($userInfo['two_id'] && $tjyj) {
                                                            $userModel::money($tjyj, $userInfo['two_id'],
                                                                '推荐有奖', $v['id'], $packageInfo['id']);
                                                        }
                                                    }
                                                    if ($v['agency_area_id'] && $pplots_profit) {
                                                        $userModel::money($pplots_profit, $v['agency_area_id'],
                                                            '区县运营分润', $v['id'],
                                                            $packageInfo['id']);
                                                    }
                                                    if ($v['plot_id'] && $pplots_yunying_profit) {
                                                        $userModel::money($pplots_yunying_profit, $v['plot_id'],
                                                            '小区运营分润', $v['id'],
                                                            $packageInfo['id']);
                                                    }
                                                }
                                            }
                                        }
                                    } else {
                                        if ($data['state'] == 4) {

                                            $orderInfo->order_status = 0;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (isset($data['number'])) {
                        $orderInfo->phone = $data['number'];
                    }

                    if (isset($data['wlCompany'])) {
                        $orderInfo->shipping_company_name = $data['wlCompany'];
                    }
                    if (isset($data['wlOrderid'])) {
                        $orderInfo->shipping_no = $data['wlOrderid'];
                        if ($orderInfo->order_status == 1 || $orderInfo->order_status == 3) {
                            $orderInfo->order_status = 2;
                        }
                    }
                    if (isset($data['createtime'])) {
                        $orderInfo->callback_time = strtotime($data['createtime']);
                    }
                    if (isset($data['lgtsId'])) {
                        $orderInfo->lgtsId = $data['lgtsId'];
                    }
                    if (isset($data['info'])) {
                        $orderInfo->remark = $data['info'];
                    }
                    if (isset($data['actime'])) {
                        $orderInfo->actime = $data['actime'];
                    }

                    $orderInfo->order_info_json = json_encode($data, true);
                    $r                          = $orderInfo->save();
                    if ($r) {
                        echo "更新成功";
                    }
//                } else {
//                    echo "签名失败";
//                }
            }
        }
    }

    public function ceshi(){
        $this->model = new \app\common\model\Order();
        $UserInfo = db('user')->where(['id'=>288])->find();
        $this->fen($UserInfo);
    }

    public function fen($UserInfo){
        exit();
        $packagesInfo['id'] = 573;
        $config                = new UserPackagesConfig();
        $packagesInfo = db('packages')->where(['id'=>$packagesInfo['id']])->find();
        if (!$UserInfo['parent_id']) {
            $money = $config->where('user_id', $UserInfo['id'])->where(['packages_id' => $packagesInfo['id']])->sum('money');
            if (!$money) {
                $money = $packagesInfo['rebate'];
            }
            $inser_two['user_id'] = $UserInfo['id'];
            $inser_two['money']   = $money;
            $inser_two['level']   = 10;//顶级代理自购
            OrderMore::create($inser_two);
        } else {
            $packages_where['packages_id'] = $packagesInfo['id'];

            if($UserInfo['three_id']){
                $zhi_money = $config->where('user_id', $UserInfo['three_id'])->where($packages_where)->value('money');
                if(!$zhi_money){
                    $zhi_money = $packagesInfo['rebate'];
                }
                $ding_money      = $config->where('user_id', $UserInfo['two_id'])->where($packages_where)->value('money');
                if($ding_money){
                    //上三层
                    $inser_two['user_id'] = $UserInfo['three_id'];
                    $inser_two['money']   = $ding_money;
                    $inser_two['level']   = 9;
                    OrderMore::create($inser_two);
                }else{
                    $ding_money = 0;
                }

                //二层
                $two_money   = $config->where('user_id', $UserInfo['parent_id'])->where($packages_where)->value('money');
                if($two_money){
                    $inser_two['user_id'] = $UserInfo['two_id'];
                    $inser_two['money']   = $two_money;
                    $inser_two['level']   = 8;
                    OrderMore::create($inser_two);
                }else{
                    $two_money = 0;
                }

                //一层
                $parent_money                  = $config->where('user_id', $UserInfo['id'])->where($packages_where)->value('money');
                if($parent_money){
                    $inser_two['user_id']          = $UserInfo['parent_id'];
                    $inser_two['money']            = $parent_money;
                    $inser_two['level']   = 7;//直推
                    OrderMore::create($inser_two);
                }else{
                    $parent_money = 0;
                }
                //自购
                $inser_two['user_id']  = $UserInfo['id'];
                $inser_two['money']    = $zhi_money - ($parent_money + $two_money + $ding_money);
                $inser_two['level']    = 6;
                OrderMore::create($inser_two);

            }elseif ($UserInfo['two_id']){
                //顶级佣金
                $zhi_money = $config->where('user_id', $UserInfo['two_id'])->where($packages_where)->value('money');
                if(!$zhi_money){
                    $zhi_money = $packagesInfo['rebate'];
                }
                //上二层
                $two_money   = $config->where('user_id', $UserInfo['parent_id'])->where($packages_where)->value('money');
                if($two_money){
                    $inser_two['user_id'] = $UserInfo['two_id'];
                    $inser_two['money']   = $two_money;
                    $inser_two['level']   = 8;
                    OrderMore::create($inser_two);
                }else{
                    $two_money = 0;
                }

                //一层
                $parent_money                  = $config->where('user_id', $UserInfo['id'])->where($packages_where)->value('money');
                if($parent_money){
                    $inser_two['user_id']          = $UserInfo['parent_id'];
                    $inser_two['money']            = $parent_money;
                    $inser_two['level']   = 7;//直推
                    OrderMore::create($inser_two);
                }else{
                    $parent_money = 0;
                }
                //自购
                $inser_two['user_id']  = $UserInfo['id'];
                $inser_two['money']    = $zhi_money -  (bcadd($parent_money, $two_money));
                $inser_two['level']    = 6;
                OrderMore::create($inser_two);
            }else{
                //顶级
                $parent_money   = $config->where('user_id', $UserInfo['parent_id'])->where($packages_where)->value('money');
                if(!$parent_money){
                    $parent_money = $packagesInfo['rebate'];
                }
                $one_money                     = $config->where('user_id', $UserInfo['id'])->where($packages_where)->value('money')??0;
                if($one_money){
                    $inser_two['user_id']          = $UserInfo['parent_id'];
                    $inser_two['money']            = $one_money;
                    $inser_two['level']            = 7;//直推
                    OrderMore::create($inser_two);
                }
//                //自购
                $inser_two['level']        = 6;//自购
                $inser_two['user_id']      = $UserInfo['parent_id'];
                $inser_two['money']        = $parent_money - $one_money;
                OrderMore::create($inser_two);
            }
        }
    }


    public function orderceshi($v){
        $userData = findAncestors($v->user_id);
        if(!$userData){
            $config = new UserPackagesConfig();
            $rebate = $config->where(['user_id'=>$v->user_id,'packages_id'=>$v->goods_id])->value('money');
            if($rebate){
                $packageInfo['rebate']  = $rebate;
            }
            //顶级代理下单
            dingjiTwo($v->user_id,$v->id,$v->goods_id,$packageInfo['rebate'],false,true);
        }else{
            array_unshift($userData,$v->user_id);
            $money = moneydigui($v->user_id,$v->goods_id);
            if($money){
                digui($v->user_id, $money,  $v->goods_id,  $v->id,0,true);
            }else{
                //顶级
                $config = new UserPackagesConfig();
                $rebate = $config->where(['user_id'=>$v->user_id,'packages_id'=>$v->goods_id])->value('money');
                if($rebate){
                    $packageInfo['rebate']  = $rebate;
                }
                dingji($userData[count($userData)-1],$v['id'],$v->goods_id,$packageInfo['rebate'],false,true);
            }
        }
    }


    public function cc(){



// 输出结果
//        print_r($result);

    }

    public function laqu(){

        $url = "https://hao.san-jk.com/agent/index/getApiConfig";//办理套餐
// 要发送的数据
        $data = db('packages')->where(['platform_type'=>2])->select();
        $num  = -1;
        foreach ($data as $k=>$v){
            $json_arr = json_decode($v['data_json'],true);
            $data = array(
                'goods_id' => $json_arr['id']
            );
            $data_string = json_encode($data);
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    'Content-Type: application/json',
                    'Content-Length: ' . strlen($data_string),
                    'Token:f4a522fa-c3a8-45b1-a1ba-876aba8657f3'
                )
            );
            $result = json_decode( curl_exec($ch),true);
            if(!empty($result['data']['list'][0]['acid'])){
               $r = db('packages')->where(['id'=>$v['id']])->update(['packages_id'=>$result['data']['list'][0]['acid'],'packages_id_two'=>$v['packages_id']]);
               if($r){
                   $num +=1;
               }
            }
        }
        dump($num);exit();

// 将数据转换为JSON格式
        $jsonData = json_encode($data);

      //初始化
        $ch = curl_init();
        $data['goods_id'] = 56;
        $params[CURLOPT_URL] = $url; //请求url地址
        $params[CURLOPT_HEADER] = false; //是否返回响应头信息
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向
        $params[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1';
        $params[CURLOPT_POST] = true;
        curl_setopt($ch, CURLOPT_POSTFIELDS,  $data); // Post提交的数据包
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($jsonData),
            'Token:43e56382-5b81-4d3a-8e65-cb6f35dd2834'
        ));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
    //执行命令
        dump($content);exit();
        $data = json_decode($content,true);


        foreach ($data['data']['list'] as $k=>$v){
            if($k>0){
                $inser['platform_type'] = 3;
                $inser['cate_id'] = 2;//联通
                $inser['photos'] = $v['image'];
                $inser['photoLists']  = $v['image'];
                $inser['h5url']       = $v['url'];
                $inser['rebate']      = $v['agfee'];
                $inser['data_json']   = json_encode($v);
                $inser['packages_id'] = $v['id'];
                $inser['name']        = $v['name'];
                $inser['status']      = 1;
                \app\admin\model\Packages::create($inser);

            }

        }
        exit();
   $this->success('',$content);
    //关闭URL请求

//    curl_close($curl);


}

    public function imagebase64(){
        $image = input('image');
        if(!$image){
            $this->error('缺少参数');
        }
        if(strpos($image, 'https') === false || strpos($image, 'http') === false){
            $image = '.'.$image;
        }
        $imageData = file_get_contents($image);
        $base64Image = base64_encode($imageData);
        $this->success('ok',$base64Image);
    }

    /**
     * 首页
     *
     */
    public function index()
    {
//        $cate_id         = input('cate_id');
        $where['status'] = 1;
        $agent_id        = input('agent_id');
        if(!$agent_id){
            $this->error('缺少参数');
        }
        $agent_info  = \app\common\model\User::where(['id'=>$agent_id,'parent_id'=>$this->auth->id])->find();
        if(!$agent_info){
            $this->error('代理不存在');
        }
//        if($cate_id){
//            $where['cate_id'] = $cate_id;
//        }

        $state = input('state');
        if($state == 2){
            $goods_soldout = db('packages_user_status')->where(['agent_id'=>$agent_id])->column('packages_id');
            $data = \app\admin\model\Packages::where($where)->whereIn('type','1,2')->whereIn('id',$goods_soldout)->field('id,packages_id,name,cate_id,photos,rebate,h5url,platform_type,card_type,user_memo')->order('weigh desc')->paginate();
        }else{
            $query = \app\admin\model\Packages::where($where)->whereIn('type','1,2');
            $goods_soldout = db('packages_user_status')->where(['agent_id'=>$agent_id])->column('packages_id');
            if($goods_soldout){
                $query = $query->whereNotIn('id', $goods_soldout);
            }
            $data = $query->field('id,packages_id,name,cate_id,photos,rebate,h5url,platform_type,card_type,user_memo')->order('weigh desc')->paginate();

        }
        $data = $this->compute($data->toArray(),$agent_id,2);
        $this->success('请求成功',$data);
    }

    public function compute($data,$agent_id,$is_dl=1){
        $config = new UserPackagesConfig();
        foreach ($data['data'] as $k=>&$v){
            $v['junior_rebate'] = 0;
                if($is_dl == 1){
                    if(!$this->auth->parent_id){
                        $money = $config->where('user_id', $agent_id)->where(['packages_id'=>$v['id']])->value('money');
                        if($money){
                            $v['junior_rebate'] = $money;
                        }else{
                            $v['junior_rebate'] = $v['rebate'];
                        }
                    }else{
                        //拿到顶级佣金
                        if($this->auth->three_id){
                            $zhi_money = $config->where('user_id', $this->auth->three_id)->where(['packages_id'=>$v['id']])->value('money');
                            if(!$zhi_money){
                                $zhi_money = $v['rebate'];
                            }
                            $zhi_money -= $config->where('user_id', $this->auth->two_id)->where(['packages_id'=>$v['id']])->value('money');
                            $zhi_money -= $config->where('user_id', $this->auth->parent_id)->where(['packages_id'=>$v['id']])->value('money');
                        }elseif ($this->auth->two_id){
                            $zhi_money = $config->where('user_id', $this->auth->two_id)->where(['packages_id'=>$v['id']])->value('money');
                            if(!$zhi_money){
                                $zhi_money = $v['rebate'];
                            }
                            $zhi_money -= $config->where('user_id', $this->auth->parent_id)->where(['packages_id'=>$v['id']])->value('money');
                        }else{
                            $zhi_money = $config->where('user_id', $this->auth->parent_id)->where(['packages_id'=>$v['id']])->value('money');
                            if(!$zhi_money){
                                $zhi_money = $v['rebate'];
                            }
                        }
                        $zhi_money -= $config->where('user_id', $agent_id)->where(['packages_id'=>$v['id']])->value('money');
                        $v['junior_rebate'] = $zhi_money;
                        if($v['junior_rebate'] < 0){
                            $v['junior_rebate'] = 0;
                        }
                    }
                }else{
                    $v['is_soldout']  = PackagesUserStatus::where([
                        'agent_id'    => $agent_id,
                        'packages_id' => $v['id']
                    ])->count();
                    $v['junior_rebate'] = $config->where('user_id',$agent_id)->where(['packages_id'=>$v['id']])->sum('money');
                    //拿到顶级佣金
                    if($this->auth->three_id){
                        $zhi_money = $config->where('user_id', $this->auth->three_id)->where(['packages_id'=>$v['id']])->value('money');
                        if(!$zhi_money){
                            $zhi_money = $v['rebate'];
                        }
                        $zhi_money -= $config->where('user_id', $this->auth->two_id)->where(['packages_id'=>$v['id']])->value('money');
                        $zhi_money -= $config->where('user_id', $this->auth->parent_id)->where(['packages_id'=>$v['id']])->value('money');
                        $zhi_money -= $config->where('user_id', $this->auth->id)->where(['packages_id'=>$v['id']])->value('money');
                    }elseif ($this->auth->two_id){
                        $zhi_money = $config->where('user_id', $this->auth->two_id)->where(['packages_id'=>$v['id']])->value('money');
                        if(!$zhi_money){
                            $zhi_money = $v['rebate'];
                        }
                        $zhi_money -= $config->where('user_id', $this->auth->parent_id)->where(['packages_id'=>$v['id']])->value('money');
                        $zhi_money -= $config->where('user_id', $this->auth->id)->where(['packages_id'=>$v['id']])->value('money');
                    }elseif ($this->auth->parent_id){
                        $zhi_money = $config->where('user_id', $this->auth->parent_id)->where(['packages_id'=>$v['id']])->value('money');
                        if(!$zhi_money){
                            $zhi_money = $v['rebate'];
                        }
                        $zhi_money -= $config->where('user_id', $this->auth->id)->where(['packages_id'=>$v['id']])->value('money');
                    }else{
                        $zhi_money = $config->where('user_id', $agent_id)->where(['packages_id'=>$v['id']])->value('money');
                    }
                    if(!$this->auth->parent_id){
                        $money = $config->where('user_id', $this->auth->id)->where(['packages_id'=>$v['id']])->value('money');
                        if($money){
                            $v['user_rebate'] = $money - $zhi_money;
                        }else{
                            $v['user_rebate'] = $v['rebate'] - $zhi_money;
                        }
                        if(!$v['junior_rebate']){
                            if(!$money){
                                $money = $v['rebate'];
                            }
                            $v['junior_rebate']  = $money;
                            $v['user_rebate'] = 0;
                        }
                    }else{
                        if($v['junior_rebate']){
                            $zhi_money -= $v['junior_rebate'];
                        }else{
                            $v['junior_rebate'] = $zhi_money;
                            $zhi_money = 0;
                        }
                        $v['user_rebate'] = $zhi_money;
                        if($v['user_rebate'] < 0){
                            $v['user_rebate'] = 0;
                        }
                    }

                }
            if (strpos($v['photos'], 'http') === false) {
                $v['photos'] = cdnurl($v['photos'],true);
            }
            $v['url'] = '';
        }
        return $data;
    }


    public function goods(){
        $model = new \app\admin\model\Packages();
        $cate_id         = input('cate_id','');
        $card_type       = input('card_type');
        $day_value       = input('day_value');
        $jiesuan_type    = input('jiesuan_type');
        $is_extend       = input('is_extend');
        $top_money_value = input('top_money_value');
        $balance_type    = input('balance_type');
        $where['status'] = 1;
        if($cate_id){
            if($cate_id == 1){
                $where['is_extend']    = 2;
            }else if($cate_id == 3){
                $where['cate_id']    = 1;
            }else if($cate_id == 4){
                $where['cate_id']    = 3;
            }else if($cate_id == 5){
                $where['cate_id']    = 4;
            }else{
                $where['cate_id']    = $cate_id;
            }

        }
        if($is_extend){
            $where['is_extend'] = $is_extend;
        }
        if($card_type){
            $where['card_type'] = $card_type;
        }
        if($jiesuan_type){
            $where['jiesuan_type'] = 2;
        }
        if($top_money_value){
            $where['top_up'] = $top_money_value;
        }
        $goods_soldout = db('packages_user_status')->where(['agent_id'=>$this->auth->id])->column('packages_id');
        $query = $model->where($where)->whereIn('type','1,2');
        if($this->auth->parent_id){
            $query = $query->whereIn('id', $goods_soldout);
        }
        if($balance_type == 1){
            $query = $query->whereIn('day', '1,2,3,4,5,7');
        }
        if($balance_type == 2){
            $query = $query->where('day', '=',15);
        }
        if($balance_type == 3){
            $query = $query->where('day', '=',6);
        }

        $data = $query->field('id,packages_id,jsgz,spxz,name,cate_id,photos,rebate,h5url,monthly,flow,call_minutes,pact,demand,age,is_extend,platform_type,status,card_type,user_memo,day')->order('weigh desc')->paginate();
        $data = $this->compute($data->toArray(),$this->auth->id);
        foreach ($data['data'] as $k=>&$v){
            $v['web_url_text'] = 'https://'.$_SERVER['HTTP_HOST'].'/qing/#/pages/order/order_details?id='.$v['id'].'&agid='.$this->auth->id;
            if (strpos($v['photos'], 'http') === false) {
                $v['photos'] = cdnurl($v['photos'],true);
            }
        }
        $config = db('config')->where(['name'=>'dailisendmsg'])->find();
        $data['tongzhi'] = $config['value'];
        $data['zhongyaotongzhi']    = config('site.zhongyaotongzhi');
        $user_msg      = db('msg_status')->where(['user_id'=>$this->auth->id,'config_id'=>$config['id']])->find();
        if($user_msg){
            $data['flag']    = false;
        }else{
            $inser['user_id']    = $this->auth->id;
            $inser['config_id']  = $config['id'];
            $inser['createtime'] = date('Y-m-d H:i:s');
            db('msg_status')->insert($inser);
            $data['flag']    = true;
        }
//        if($this->auth->id ==2){
//            $data['flag']    = true;
//        }
        $data['top_list']    = $model->getTopUpList();
        $data['top_list'][0] = "全部";
        $this->success('ok',$data);
    }

    public function goodsdetails(){
        $ids = input('packages_id');
        if(!$ids){
            $this->error('缺少参数');
        }
        $info = Packages::where(['id'=>$ids])->find()->toArray();
        if(!$info){
            $this->error('产品不存在');
        }
        if($info['status'] !=1){
            $this->error('产品已被下架');
        }
        if($info['platform_type'] == 1){
            if($info['data_json']){
                $data = json_decode($info['data_json'],true);
            }else{
                $photos = $info['photos'];
                unset($info['photos']);
                $data = $info;
                $data['theme_config']['bg']         = "#000000";
                $arr = explode(',', $info['photoLists']);
                foreach ($arr as $k=>$v){
                    if (strpos($v, 'http') === false) {
                        $arr[$k] = cdnurl($v,true);
                    }
                }
                if (strpos($photos, 'http') === false) {
                    $photos = cdnurl($photos,true);
                }
                $data['theme_config']['photoLists'] = $arr;
                $data['photos'][0]                  = $photos;
            }

            if($info['cate_id']==4){
                $data['opText'] = '广电';
            }else if($info['cate_id']==5){
                $data['opText'] = '宽带';
            }
            $data['platform_type'] = $info['platform_type'];

        }else{
            if($info['cate_id'] == 1){
                $info['opText'] = '电信';
            }else if($info['cate_id']==2){
                $info['opText'] = '联通';
            }else if($info['cate_id']==3){
                $info['opText'] = '移动';
            }else if($info['cate_id']==4){
                $info['opText'] = '广电';
            }else if($info['cate_id']==5){
                $info['opText'] = '宽带';
            }
            $photos = $info['photos'];
            unset($info['photos']);
            $data = $info;
            $data['theme_config']['bg']         = "#000000";
            $arr = explode(',', $info['photoLists']);
            foreach ($arr as $k=>$v){
                if (strpos($v, 'http') === false) {
                    $arr[$k] = cdnurl($v,true);
                }
            }
            if (strpos($photos, 'http') === false) {
                $photos = cdnurl($photos,true);
            }
            $data['theme_config']['photoLists'] = $arr;
            $data['photos'][0]                  = $photos;
        }
        if($info['platform_type'] == 4){
//            $data['platform_type'] = 1;
        }
        $data['spxz'] = $info['spxz'];
        $data['jsgz'] = $info['jsgz'];
        $this->success('ok',$data);
    }

    /**
     * 下架
     */
    public function soldout(){
        $ids         = input('ids');
        $agent_id    = input('agent_id');
        $status      = input('status');
        $isRecords   = input('isRecords',0);
        if($isRecords){
            if(!$ids || !in_array($status, [1,2])){
                $this->error('参数错误');
            }
            $arr = explode(',', $ids);
            $packagesArr =  \app\admin\model\Packages::where(['status'=>1])->whereIn('type','1,2')->field('id,day,rebate')->column('id');
            $packagesIds = implode(',', $packagesArr);
            foreach ($arr as $k=>$v){
                soldout($status,$packagesIds,$v);
            }
        }else{
            if(!$agent_id || !$ids || !in_array($status, [1,2])){
                $this->error('参数错误');
            }
            soldout($status,$ids,$agent_id);
        }
        $this->success('ok');
    }



    public function zqRange(){
        $model = new \app\admin\model\Packages();
        $agent_id    = input('agent_id');
        $packages_id = input('packages_id');
        $config = UserPackagesConfig::where(['user_id'=>$this->auth->id,'packages_id'=>$packages_id])->find();
        if($config){
            $day = $config['day'];
        }else{
            $day = $model->where(['id'=>$packages_id])->value('day');
        }
        $list  =  $model->getDayList();
        $arr   = [];
        $num   = -1;
        foreach ($list as $k=>$v){
            if($k >= $day){
                $num ++;
                $arr[$num]['id']   = $k;
                $arr[$num]['name'] = $v;
            }

        }
        $this->success('ok',$arr);
    }

    public function msgsend(){
        $model = new Msgsned();
        $m_type = input('m_type',1);
        $ids    = input('ids');
        if($ids){
            $where['id'] = $ids;
        }else{
            $where['type'] = 1;
            $where['m_type'] = $m_type;
        }
        $data  = $model->where($where)->order('weigh desc')->select();
        $this->success('ok',$data);
    }

    /**
     * @menu 详情
     * @Author:男人的坏
     * @Date:2024-05-12 17:59
     */

    public function msgsendInfo(){
        $model = new Msgsned();
        $ids = input('ids');
        $data  = $model->where(['id'=>$ids])->order('weigh desc')->find();
        $this->success('ok',$data);
    }

    public function msgsend_details(){
        $model = new Msgsned();
        $ids   = input('ids');
        if(!$ids){
            $this->error('缺少参数');
        }
        $info = $model->where(['id'=>$ids])->find();
        $this->success('ok',$info);
    }

    public function config(){
        $ids = input('id');
        $contet = '';
        if($ids){
            $model = new Msgsned();
            $info = $model->where(['id'=>$ids])->find();
            $contet = $info['content'];
        }
        $hehuoren             = config('site.user_partner_content');
        $user_yongjin_content = config('site.user_yongjin_content');
        $user_yztc_content    = config('site.user_yztc_content');
        $qiri_content         = config('site.qiri_content');
        $yugu_content         = config('site.yugu_content');
        $ciyue_content        = config('site.ciyue_content');
        $xiaji_yongjin_content   = config('site.xiaji_yongjin_content');
        $this->success('ok',[
            'hehuoren'             => $hehuoren,
            'user_yongjin_content' => $user_yongjin_content,
            "user_yztc_content"    => $user_yztc_content,
            'content'              => $contet,
            'qiri_content'        => $qiri_content,
            'yugu_content'        => $yugu_content,
            'ciyue_content'        => $ciyue_content,
            'xiaji_yongjin_content'=>$xiaji_yongjin_content
        ]);
    }

    /**
     * @menu 轮播图接口
     * @Author:男人的坏
     * @Date:2024-05-12 20:25
     */
    public function banner()
    {
        $model = new Banner();
        $data  = $model->order('weigh desc')->select();
        $this->success('ok', $data);
    }

    /**
     * @menu 轮播图详情
     * @Author:男人的坏
     * @Date:2024-05-12 20:33
     */
    public function banner_details()
    {
        $ids = input('ids');
        if (!$ids) {
            $this->error('缺少参数');
        }
        $info = Banner::where(['id' => $ids])->find();
        $this->success('ok', $info);
    }


}
